<template>
  <div style="margin-top: 5px;">
    <quill-editor
      class="editor"
      v-model="content"
      :options="editorOption"
      @change="onEditorChange($event)"
      ref="quill"
    />
    <input
    style="display: none;"
      id="quill-img"
      class="quill-img"
      type="file"
      @change="fileChange($event)"
    />
  </div>
</template>

<script>
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { uploadImg,checkMediaType } from "@/utils";
export default {
  components: {
    quillEditor,
  },
  data() {
    return {
      content: "",
      uploader: null,
      file: null,
      editorOption: {
        // 编辑器的配置选项
        placeholder:'请输入内容',
        modules: {
          toolbar: {
            container: [
              ["bold", "italic", "underline", "strike"], // 加粗，斜体，下划线，删除线
              ["blockquote", "code-block"], // 引用，代码块
              [{ header: 1 }, { header: 2 }], // 1 个头，2 个头
              [{ list: "ordered" }, { list: "bullet" }],
              [{ script: "sub" }, { script: "super" }], // 上下标
              [{ indent: "-1" }, { indent: "+1" }], // 缩进
              [{ direction: "rtl" }], // 文本方向
              [{ size: ["small", false, "large", "huge"] }], // 字号
              [{ header: [1, 2, 3, 4, 5, 6, false] }],
              [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
              [{ font: [] }],
              [{ align: [] }],
              ["clean"], // 清除格式
              ["image"],
            ],
            handlers: {
              image: function (value) {
                if (value) {
                  // 触发input框选择图片文件
                  document.getElementById("quill-img").click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
        },
      },
    };
  },
  methods: {
    set(data){
      this.content = data
    },
    fileChange(e) {
      console.log(e.target.files[0]);
      uploadImg(e.target.files[0],null,{},'light',(res)=>{
    
        this.quillImgSuccess(res)
        // this.quillImgSuccess('https://www.dideng.cc/AttFile/Product/ProductImg/401_20250808112010_0.jpeg')
      })
    },
    quillImgSuccess(res) {
      let quill = this.$refs.quill.quill;
      // 获取光标所在位置
      let length = quill.getSelection().index;
      // 插入图片 或视频
      console.log(checkMediaType(res))
      if(checkMediaType(res)=='image'){
        quill.insertEmbed(length, "image", res);
      }else{
        quill.insertEmbed(length, "video", res);
      }
      // 调整光标到最后
      quill.setSelection(length + 1);
    },
    onEditorChange({ html, text, quill }) {
      console.log(html, text, quill);
    },
  },
};
</script>

<style lang="scss" scoped></style>
